package cfgs.gestudent.finestra.curs;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import cfgs.gestudent.dao.curs.EliminarCurs;
import cfgs.gestudent.dao.curs.MostrarCursos;
import cfgs.gestudent.dao.curs.TaulaControladoraModificacionsCurs;
import cfgs.gestudent.preferencies.ConfiguracionsIdiomes;
import cfgs.gestudent.preferencies.PreferenciesAnyAcademic;

import java.awt.GridBagLayout;
import java.util.ResourceBundle;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class AdministrarCursos extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4183066875388553916L;
	
	private ConfiguracionsIdiomes configs = new ConfiguracionsIdiomes();
	private ResourceBundle rb = configs.getIdioma();
	
	private JTable table;
	private JScrollPane scpTable = null;
	private DefaultTableModel dtmEjemplo = null;
	private JTextField textField;
	private MostrarCursos mc;
	
	private JPopupMenu popupMenu;
	private JMenuItem mntmBorrar;

	/**
	 * Create the panel.
	 */
	public AdministrarCursos() {
		
		setBackground(Color.white);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {0, 30, 30, 30, 30, 30, 0, 0};
		gridBagLayout.rowHeights = new int[] {30, 30};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{Double.MIN_VALUE, 1.0};
		setLayout(gridBagLayout);
		
		table = new JTable();
		GridBagConstraints gbc_table = new GridBagConstraints();
		gbc_table.fill = GridBagConstraints.BOTH;
		gbc_table.gridwidth = 7;
		gbc_table.gridx = 0;
		gbc_table.gridy = 1;
		scpTable = new JScrollPane();
		scpTable.setViewportView(table);
		add(scpTable, gbc_table);
		table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		mc = new MostrarCursos();
		String sql = "SELECT c.Nom, c.Id FROM Curs c, Any_ACADEMIC aa WHERE c.Id_Any_Academic=aa.Id AND aa.Id="+veureIdAnyAncademic();
		mc.setSql(sql);
		
		dtmEjemplo = new DefaultTableModel(mc.getFilas(), mc.getColumnas());
		table.setModel(dtmEjemplo);
		table.getColumnModel().getColumn(0).setCellEditor(new TaulaControladoraModificacionsCurs(mc.getFilas(), "Nom", 1));
		
		textField = new JTextField();
		textField.getDocument().addDocumentListener(new DocumentListener() {
			
			@Override
			public void removeUpdate(DocumentEvent arg0) {
				String sql = "SELECT c.Nom, c.Id FROM Curs c, Any_ACADEMIC aa WHERE c.Id_Any_Academic=aa.Id AND aa.Id="+veureIdAnyAncademic()+" AND c.Nom LIKE '%"+textField.getText()+"%'";
				mc.setSql(sql);
				dtmEjemplo = new DefaultTableModel(mc.getFilas(), mc.getColumnas());
				table.setModel(dtmEjemplo);
				table.getColumnModel().getColumn(0).setCellEditor(new TaulaControladoraModificacionsCurs(mc.getFilas(), "Nom", 1));
			}
			
			@Override
			public void insertUpdate(DocumentEvent arg0) {
				String sql = "SELECT c.Nom, c.Id FROM Curs c, Any_ACADEMIC aa WHERE c.Id_Any_Academic=aa.Id AND aa.Id="+veureIdAnyAncademic()+" AND c.Nom LIKE '%"+textField.getText()+"%'";
				mc.setSql(sql);
				dtmEjemplo = new DefaultTableModel(mc.getFilas(), mc.getColumnas());
				table.setModel(dtmEjemplo);
				table.getColumnModel().getColumn(0).setCellEditor(new TaulaControladoraModificacionsCurs(mc.getFilas(), "Nom", 1));
			}
			
			@Override
			public void changedUpdate(DocumentEvent arg0) {
				String sql = "SELECT c.Nom, c.Id FROM Curs c, Any_ACADEMIC aa WHERE c.Id_Any_Academic=aa.Id AND aa.Id="+veureIdAnyAncademic()+" AND c.Nom LIKE '%"+textField.getText()+"%'";
				mc.setSql(sql);
				dtmEjemplo = new DefaultTableModel(mc.getFilas(), mc.getColumnas());
				
				table.setModel(dtmEjemplo);
				table.getColumnModel().getColumn(0).setCellEditor(new TaulaControladoraModificacionsCurs(mc.getFilas(), "Nom", 1));
			}
		});
		
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.insets = new Insets(0, 0, 5, 5);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 5;
		gbc_textField.gridy = 0;
		add(textField, gbc_textField);
		textField.setColumns(10);
		
		popupMenu = new JPopupMenu();
		this.addPopup(table, popupMenu);
		
		mntmBorrar = new JMenuItem(rb.getString("borrar"));
		mntmBorrar.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int row = table.getSelectedRow();
				Object [][] obj = mc.getFilas();
				int idBorrar = (int) obj[row][1];
				EliminarCurs ea = new EliminarCurs();
				ea.eliminarRegistre(idBorrar, table.getValueAt(row, 0).toString());
				String sql = "SELECT c.Nom, c.Id FROM Curs c, Any_ACADEMIC aa WHERE c.Id_Any_Academic=aa.Id AND aa.Id="+veureIdAnyAncademic();
				mc.setSql(sql);
				dtmEjemplo = new DefaultTableModel(mc.getFilas(), mc.getColumnas());
				table.setModel(dtmEjemplo);
				table.getColumnModel().getColumn(0).setCellEditor(new TaulaControladoraModificacionsCurs(mc.getFilas(), "Nom", 1));
			}
		});
		popupMenu.add(mntmBorrar);
		
	}
	
	private int veureIdAnyAncademic(){
		int idAny = 0;
		
		PreferenciesAnyAcademic paa = new PreferenciesAnyAcademic();
		idAny = paa.getAnyAcademic();
		
		return idAny;
	}
	
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}

}
